package samples.d2.shapes 
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import samples.BaseSample;
	import shapes.shapes2D.polygon.RegularPolygon;
	
	/**
	 * ...
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class RegularPolygon5 extends BaseSample
	{
		private var poly:RegularPolygon;
		private var count:int = 0;
		private var canvas:Shape = new Shape();
		public function RegularPolygon5() 
		{
			
			poly = new RegularPolygon( sw / 2, sh / 2, 150, 3 );
			poly.draw();
			addChild( poly );
			
			
			addChild( canvas );
			//canvas.x = sw / 2;
			//canvas.y = sh / 2;
			
			
			addEventListener( Event.ENTER_FRAME, update );
			addEventListener( MouseEvent.MOUSE_DOWN, changa );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemove );
			
		}
		
		private function onRemove(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			removeEventListener( Event.ENTER_FRAME, update );
			removeEventListener( MouseEvent.MOUSE_DOWN, changa );
			
		}
		
		private function changa(e:MouseEvent):void 
		{
			poly.sides++;
			if( poly.sides >= 10 ) poly.sides = 3;
			
		}
		
		private function update(e:Event):void 
		{
			var m:Point = new Point( mouseX, mouseY );
			
			canvas.graphics.clear();
			
			poly.fillColor = 0xFFFFFF;
			if ( poly.contains( m ) )
			{
				poly.fillColor = 0xFFCC00;
			}
			else
			{
			
				var p:Point = poly.getEdgeDistanceToPoint( m );
				
				canvas.graphics.lineStyle( 2, 0xFFFFFF );
				canvas.graphics.moveTo( p.x, p.y );
				canvas.graphics.lineTo( _stage.mouseX, _stage.mouseY );
				
				canvas.graphics.lineStyle( 2, 0x0000FF );
				canvas.graphics.moveTo( p.x, p.y );
				canvas.graphics.lineTo( poly.x, poly.y );
				
				canvas.graphics.lineStyle( 3, 0xFF0000 );
				canvas.graphics.drawCircle( p.x, p.y, 10 );
				
			}
				poly.draw();
				
		}
		
	}

}